﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ResearchRepository.Models;

namespace ResearchRepository
{
	public partial class RRRegister : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			Title = GLOBALS.masterTitlePrefix + " - Registration Request";
			// if already logged in, then redirect to account page
			if (Session[GLOBALS.userObjSessionVar] != null)
			{
				Response.Redirect("Account.aspx");
			}
			//TODO??
			/*if (IsPostBack)
			{
			}*/
		}
		public void OnClickUniversity(object sender, EventArgs e)
		{
			// option 1: none
			departmentRow.Visible = !universitynone.Checked;

			// option 2: from list
			// load university list
			if (universityfromlist.Checked)
			{
				universitydrop.DataSource = RRDatabaseManager.getUniversityList();
				universitydrop.DataBind();
			}
			// show dropdown after load
			universitydrop.Visible = universityfromlist.Checked;

			// option 3: new
			newUniversityDiv.Visible = universitynew.Checked;

			// department 'from list' option is available only if university 'from list' is selected and the selected university has registered departments
			departmentfromlist.Enabled = universityfromlist.Checked && universitydrop.DataSource != null;
			// if department 'from list' option is already selected
			if (departmentfromlist.Checked)
			{
				// if enabled, then populate
				if (departmentfromlist.Enabled)
				{
					populateDepartmentDropdown();
				}
				// else not enabled, so if department options are visible, then deselect disabled button (don't deselect if already hidden by university 'none' option)
				else if(universitynew.Checked)
				{
					departmentnone.Checked = true;
					departmentfromlist.Checked = false;
					departmentdrop.Visible = false;
				}
			}
		}
		public void OnSelectUniversity(object sender, EventArgs e)
		{
			// update department dropdown
			populateDepartmentDropdown();
		}
		public void OnClickDepartment(object sender, EventArgs e)
		{
			// option 1: none
			// no associated side-effects

			// option 2: from list
			// load department list
			populateDepartmentDropdown();
			// show dropdown after load
			departmentdrop.Visible = departmentfromlist.Checked;

			// option 3: new
			newDepartmentDiv.Visible = departmentnew.Checked;
		}
		protected void populateDepartmentDropdown()
		{
			if (departmentfromlist.Checked && universitydrop.SelectedItem != null)
			{
				departmentdrop.DataSource = RRDatabaseManager.getDepartmentList(int.Parse(universitydrop.SelectedValue));
				departmentdrop.DataBind();
			}
		}
		public void OnClickSubmit(object sender, EventArgs e)
		{
			RRRequest request = new RRRequest();
			request.ReqType = RRRequestType.NewUser;
			request.UserID = RRUser.GuestUserID;
			request.Note = notetext.Text;

			request.NewUserEmail = emailtext.Text;
			request.NewUserPreTitles = prefixtext.Text;
			request.NewUserFirstName = firsttext.Text;
			request.NewUserMiddleName = midtext.Text;
			request.NewUserLastName = lasttext.Text;
			request.NewUserPostTitles = postfixtext.Text;

			if (!universitynone.Checked)
			{
				if (universityfromlist.Checked)
				{
					request.NewUnivName = universitydrop.SelectedItem.Text;
					request.UnivID = int.Parse(universitydrop.SelectedValue);
				}
				else
				{
					request.NewUnivName = universitynewtext.Text;
				}
				if (!departmentnone.Checked)
				{
					if (departmentfromlist.Checked)
					{
						request.NewDeptName = departmentdrop.SelectedItem.Text;
						request.DeptID = int.Parse(departmentdrop.SelectedValue);
					}
					else
					{
						request.NewDeptName = departmentnewtext.Text;
					}
				}
			}
			try
			{
				//TODO?: check for existing email?

				RRDatabaseManager.createRequest(request);
				//TODO: send confirmation email using System.Net.Mail namespace
				successDiv.Visible = true;
				registrationTable.Visible = false;
			}
			catch (Exception x)
			{
				//TODO?? handle?
				failureDiv.Visible = true;
			}
		}
	}
}